package com.ycky.common.utils.bean;

import org.springframework.validation.Validator;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: bean对象属性验证
 * @author: cjj
 * @date: 2025/3/25 13:27
 **/
public class BeanValidators {
    public static void validateWithException(Validator validator, Object object) {
        org.springframework.validation.BeanPropertyBindingResult errors = new org.springframework.validation.BeanPropertyBindingResult(object, object.getClass().getName());
        validator.validate(object, errors);
        if (errors.hasErrors()) {
            List<String> errorMessages = new ArrayList<>();
            errors.getAllErrors().forEach(error -> errorMessages.add(error.getDefaultMessage()));
            throw new IllegalArgumentException(String.join("; ", errorMessages));
        }
    }
}